import ajax from "@/lib/request/request.js"
import wxjssdk from "../../lib/wxh5-tools.js"
export default {
	namespaced: true,
	state: {
		userInfo: "",
		token: ""
	},
	mutations: {
		setAuth(state, obj) {
			state.userInfo = obj.userInfo;
			state.token = obj.token;
			uni.setStorageSync("token", obj.token)
			uni.setStorageSync("userInfo", obj.userInfo)
			console.log("判断是否有跳转参数")
			// 判断是否有跳转参数
			let redirect_url = uni.getStorageSync("redirect_url");
			console.log("判断是否有跳转参数",redirect_url)
			if(redirect_url){
				uni.navigateTo({
					url:redirect_url
				})
			}
		}
	},
	actions: {
		async wxLoginByCode(ctx, code) {
			uni.showLoading({
				title: '请稍后'
			})
			let res = await ajax.post({
				url: "/wechat/wxRigistByCode",
				data: {
					code: code
				}
			})
			uni.hideLoading()
			console.log("wxLoginByCode", res)
			let token = res.openid;
			let userInfo = res.data
			ctx.commit("setAuth", {
				token,
				userInfo
			})

			
		},
		async getUserInfoByToken(ctx,token){
			uni.showLoading({
				title: '请稍后'
			})
			let res = await ajax.get({
				url: "/wechat/getUserInfoByToken",
			})
 
			uni.hideLoading()
			ctx.commit("setAuth", {
				token,
				userInfo:res.data
			})
		},
		async initJSsdk(ctx){
			// 初始化jssdk
			console.log("初始化jssdk")
			let jssdkSign = await ajax.post({
				url: "/wechat/wxh5JssdkSign",
				data: {
					share_url: window.location.href.split("#")[0]
				}
			})
			console.log(jssdkSign)
				wxjssdk.init({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: jssdkSign.data.appid, // 必填，公众号的唯一标识
					timestamp: jssdkSign.data.timestamp, // 必填，生成签名的时间戳
					nonceStr: jssdkSign.data.noncestr, // 必填，生成签名的随机串
					signature: jssdkSign.data.signature, // 必填，签名
					jsApiList: ['getLocation','chooseWXPay','scanQRCode'] // 必填，需要使用的JS接口列表
				})
		},
		async bindAccount(ctx, data) {
			uni.showLoading({
				title:"绑定中"
			})
			let res = await ajax.post({
				url: "/wechat/bindAccount",
				data: {
					...data
				}
			}).catch(err=>{
				uni.hideLoading()
			})
			uni.hideLoading()
			return res;
		},
		async unBind(ctx) {
			let res = await ajax.get({
				url: "/wechat/unBindAccount",
			})
			ctx.commit("setAuth", {
				token: "",
				userInfo: ""
			})
			return res;
		},
		// 签到
		async sign(ctx,params){
			let res = await ajax.post({
				url:"/wechat/sign",
				data:{
					...params
				}
			})
			return res
			
		},
		// 获取打卡位置列表
		async getSignPosition(){
			let res = await ajax.get({
				url:"/wechat/getSignPosition",
			 
			})
			return res
		}
	
	
	}
}
